//
//  XHEssenceViewController.m
//  BSNoAnswer
//
//  Created by Tiffany宝 on 16/5/12.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

#import "XHEssenceViewController.h"
#import "XHTopicViewController.h"

@interface XHEssenceViewController ()<UIScrollViewDelegate>

@property (nonatomic,weak) UIView *redIndicator;
@property (nonatomic,weak) UIView *titleView;
@property (nonatomic,weak) UIButton *selectedButton;
@property (nonatomic,weak) UIScrollView *contentView;

@end

@implementation XHEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航栏
    [self setupNav];
    //设置子控制器
    [self setupChildVc];
    
    //设置顶部的标签栏
    [self setupTitlesView];
    
    //添加scrollView
    [self setupContentView];
   
}

#pragma mark -
#pragma mark 设置顶部的标签栏
-(void)setupTitlesView{
    
    UIView *titleView = [[UIView alloc]init];
//    titleView.tag = 20000;
    titleView.width = self.view.width;
    titleView.height = XHTitleViewH;
    titleView.y = XHNavBarMaxY;
    //修改颜色(白色)的透明度(三种方法)
    titleView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.7];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    //添加红色标签指示器
    UIView *redIndicator = [[UIView alloc]init];
    self.redIndicator = redIndicator;
    redIndicator.backgroundColor = [UIColor redColor];
    redIndicator.height = 2;
    redIndicator.y = titleView.height - redIndicator.height;
    
    //    XHLog(@"redIndicator%@",NSStringFromCGRect(self.redIndicator.frame));
    
    //添加顶部标签栏的按钮
//    NSArray *titles = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    //添加按钮
    NSInteger count = self.childViewControllers.count;
    CGFloat buttonX;
    CGFloat buttonY = 0;
    CGFloat buttonW = titleView.width / count;
    CGFloat buttonH = titleView.height;
    
    for (int i = 0; i < count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        //给按钮打tag
        button.tag = i;
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        buttonX = i * buttonW;
//        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitle:self.childViewControllers[i].title forState:UIControlStateNormal];
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        //        XHLog(@"%@",NSStringFromCGRect(button.frame));
        [titleView addSubview:button];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
            //让按钮内部文字宽度去计算尺寸
            [button.titleLabel sizeToFit];
            self.redIndicator.width = button.titleLabel.width;
            self.redIndicator.centerX = button.centerX;
        }
    }
    [titleView addSubview:redIndicator];
}

-(void)titleClick:(UIButton *)button{
    //    self.selectedButton.selected = NO;
    //    button.selected = YES;
    //    self.selectedButton = button;
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    [UIView animateWithDuration:0.25 animations:^{
        // ***** 以下2句话顺序不能颠倒
        self.redIndicator.width = button.titleLabel.width;
        self.redIndicator.centerX = button.centerX;
    }];
    //    XHLog(@"redIndicator%@",NSStringFromCGRect(self.redIndicator.frame));
    //    XHLog(@"button%@",NSStringFromCGRect(button.frame));
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:NO];
    [self scrollViewDidEndScrollingAnimation:self.contentView];
}

#pragma mark -
#pragma mark scrollView的代理方法
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    //拿到索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    UITableViewController *Vc = self.childViewControllers[index];
    Vc.view.x = scrollView.contentOffset.x;
    Vc.view.y = 0; //设置控制器view的高度为0 (默认为20) --> 状态栏高度为20
    Vc.view.height = scrollView.height; //控制器view的默认高度为屏幕高度减去20
    
    //以下代码已经移到每个控制器中,因为不能确保以后的每个控制器都是 UITableViewController
//    CGFloat height = CGRectGetMaxY(self.titleView.frame);
//    CGFloat bottom =  self.tabBarController.tabBar.height;
//    Vc.tableView.contentInset = UIEdgeInsetsMake(height, 0, bottom, 0);
//    // 修改 tableView 指示条的位置 (不然会被导航条和顶部标签条覆盖)
//    Vc.tableView.scrollIndicatorInsets = Vc.tableView.contentInset;
//    Vc.tableView.backgroundColor = [UIColor darkGrayColor];
    [scrollView addSubview:Vc.view];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
    //拿到索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
//    XHLog(@"%@",self.titleView.subviews);
    [self titleClick:self.titleView.subviews[index]];
    // 因为 titleView 默认就有 tag 为0,所以当 tag 为0时,titleView 把自己给传进去了,导致了报错
//    [self titleClick:[self.titleView viewWithTag:index]];
//    XHLogFunc;
}

#pragma mark -
#pragma mark 添加scrollView
-(void)setupContentView{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *contentView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    self.contentView = contentView;
    //分页
    contentView.pagingEnabled = YES;
    //    contentView.backgroundColor = [UIColor cyanColor];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count , 0);
//    [self.view addSubview:contentView];
    [self.view insertSubview:contentView atIndex:0];
    contentView.delegate = self;
    //一进来就选中第一页
    [self scrollViewDidEndScrollingAnimation:contentView];
}

#pragma mark -
#pragma mark 设置子控制器
-(void)setupChildVc{
    //控制器都有 title 属性
    
    XHTopicViewController *all = [[XHTopicViewController alloc]init];
    all.title = @"全部";
    all.type = XHTopicViewTypeAll;
    [self addChildViewController:all];
    XHTopicViewController *video = [[XHTopicViewController alloc]init];
    video.title = @"视频";
    video.type = XHTopicViewTypeVideo;
    [self addChildViewController:video];
    XHTopicViewController *voice = [[XHTopicViewController alloc]init];
    voice.title = @"声音";
    voice.type = XHTopicViewTypeVoice;
    [self addChildViewController:voice];
    XHTopicViewController *pic = [[XHTopicViewController alloc]init];
    pic.title = @"图片";
    pic.type = XHTopicViewTypePicture;
    [self addChildViewController:pic];
    XHTopicViewController *topic = [[XHTopicViewController alloc]init];
    topic.title = @"段子";
    topic.type = XHTopicViewTypeWord;
    [self addChildViewController:topic];
}

#pragma mark -
#pragma mark 设置导航栏
-(void)setupNav{
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    //    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [button setBackgroundImage:[UIImage imageNamed:@"MainTagSubIcon"] forState:UIControlStateNormal];
    //    [button setBackgroundImage:[UIImage imageNamed:@"MainTagSubIconClick"] forState:UIControlStateHighlighted];
    //    [button sizeToFit];
    //    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    //    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highlightedImage:@"MainTagSubIconClick" target:self selector:@selector(buttonClick)];
    self.view.backgroundColor = XHGrayColor(223);
}

#pragma mark -
#pragma mark 导航栏左侧的点击事件
-(void)buttonClick{
    
    XHLogFunc;
}

@end
